package com.example.springboot.filter;

import com.example.springboot.config.repeatbody.CacheRequestBodyContent;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author:ChenZhangKun
 * @Date: 2024/3/19 21:16
 */
@Slf4j
public class RepeatBodyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            log.info("进入可重复读取requestbody过滤器");
            requestWrapper = new CacheRequestBodyContent((HttpServletRequest) servletRequest, servletResponse);
        }
        if (null == requestWrapper) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }
}
